//
//  SelectCompanyAndProjectViewController.m
//  MyProject
//
//  Created by Pei on 16/7/26.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "SelectCompanyAndProjectViewController.h"
#import "UserInfo.h"

@interface SelectCompanyAndProjectViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    UITableView *_firstTable;
    UITableView *_secondTable;
    
    NSArray *_firstDataArray;
    NSArray *_secondDataArray;
    
    NSIndexPath *_lastIndexPath1;
    NSIndexPath *_lastIndexPath2;
    
    NSString *_currentAction;
    
    NSDictionary *superDic;
}

@end

@implementation SelectCompanyAndProjectViewController

- (void)viewDidLoad {
    
    self.navTitle = @"选择";
    
    [super viewDidLoad];
    
     _lastIndexPath1 = nil;
     _lastIndexPath2 = nil;
    
    _firstTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kDeviceWidth/2.0f, kDeviceHeight-NAV_HEIGHT)];
    _firstTable.delegate = self;
    _firstTable.dataSource = self;
    _firstTable.tag = 116;
    [self.view addSubview:_firstTable];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(kDeviceWidth/2.0f, 0.0f, 0.5f, kDeviceHeight-NAV_HEIGHT);
    lineView.backgroundColor = kGreenColor;
    [self.view addSubview:lineView];
    
    
    _secondTable = [[UITableView alloc] initWithFrame:CGRectMake(kDeviceWidth/2.0f+0.5f, 0.0f, kDeviceWidth/2.0f-0.5f, kDeviceHeight-NAV_HEIGHT)];
    _secondTable.delegate = self;
    _secondTable.dataSource = self;
    _secondTable.tag = 118;
    [self.view addSubview:_secondTable];
    
    
    
    [self getCompanyListData];
}



- (void)getCompanyListData{
    //Get_Company_List
    [[YMNetLib getInstance] connectWithAction:Get_Company_List method:@"GET" withContent:[NSString stringWithFormat:@"UserID=%@",[[UserInfo getInstance] getUserID]] withBody:nil withDelegate:self];
    _currentAction = Get_Company_List;
}

- (void)getProjectListData:(NSString*)OrgCode{
    //Get_Project_List
    [[YMNetLib getInstance] connectWithAction:Get_Project_List method:@"GET" withContent:[NSString stringWithFormat:@"orgCode=%@", OrgCode] withBody:nil withDelegate:self];
    _currentAction = Get_Project_List;
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([_currentAction isEqualToString:Get_Company_List]) {
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            _firstDataArray = [body valueForKey:@"OrgList"];
            
            NSMutableArray *dddd = [[NSMutableArray alloc] initWithCapacity:3];
            [dddd removeAllObjects];
            for (NSDictionary *ttt in _firstDataArray) {
                if ([[ttt valueForKey:@"OrgLevel"] intValue] == 0) {
                    [dddd addObject:ttt];
                }
            }
            _firstDataArray = dddd;
            [_firstTable reloadData];
            
            if (_firstDataArray != nil && _firstDataArray.count >0) {
                NSDictionary *aaaaa = [_firstDataArray objectAtIndex:0];
                superDic = aaaaa;
                [self getProjectListData:[aaaaa objectForKey:@"OrgCode"]];
            }
        }
    }else if ([_currentAction isEqualToString:Get_Important_Working_List]){
        
    }
    else{
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            _secondDataArray = [body valueForKey:@"ProjectList"];
            [_secondTable reloadData];
        }
    }
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag == 116) {
        return _firstDataArray.count;
    }else{
        return _secondDataArray.count;
    }
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 116) {
        static NSString *identifier = @"first_cell";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.textColor = kRGB(163, 163, 163);
        cell.textLabel.font = kAppFont(Home_Small_Size+2);
        //cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        //cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *bgColorView = [[UIView alloc] init];
        bgColorView.backgroundColor = kRGB(226, 226, 226);
        bgColorView.layer.masksToBounds = YES;
        cell.selectedBackgroundView = bgColorView;
        
        NSDictionary *tempDic = [_firstDataArray objectAtIndex:indexPath.row];
        
        cell.textLabel.text = [tempDic valueForKey:@"OrgName"];
        
        if (indexPath.row == 0) {
            [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
        
        return cell;
    }else{
        static NSString *identifier = @"second_cell";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.textColor = kRGB(163, 163, 163);
        cell.textLabel.font = kAppFont(Home_Small_Size+2);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSDictionary *tempDic = [_secondDataArray objectAtIndex:indexPath.row];
        cell.textLabel.text = [tempDic valueForKey:@"ProjectName"];
        return cell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54.0f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 116){
        NSDictionary *tempDic = [_firstDataArray objectAtIndex:indexPath.row];
        superDic = tempDic;
        [self getProjectListData:[tempDic objectForKey:@"OrgCode"]];
    }else{
        NSInteger newRow = [indexPath row];
        NSInteger oldRow = -1;
        if (_lastIndexPath1 != nil)
        {
            oldRow = [_lastIndexPath2 row];
        }
        
        
        if (newRow != oldRow)
        {
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                        indexPath];
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexPath2];
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            
            _lastIndexPath2 = indexPath;
        }
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        NSDictionary *tempDic = [_secondDataArray objectAtIndex:indexPath.row];
        
        if ([self.delegate respondsToSelector:@selector(passData:superDic:)])
        {
            [self.delegate passData:tempDic superDic:superDic];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
